﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;

namespace YiYiTong.Rocket.Api.Biz
{
    public class PushMsgService : ApiService
    {
        #region 个人消息
        /// <summary>
        /// 个人消息列表
        /// </summary>
        public void GetPushMsgList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int msgtype = dic.GetValue<int>("msgtype", -10);//-10表示获取所有消息
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);


            Tuple<bool, PushMsgListModel> tupleResult = PushMsgBiz.Instance.GetPushMsgList(apiContext.CurrUserId, msgtype, pageindex, pagerows);
            if (tupleResult.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "个人消息列表为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 设置消息已读
        /// </summary>
        public void ReadPushMsg(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            long msgid = dic.GetValue<long>("msgid");

            var result = PushMsgBiz.Instance.ReadPushMsg(apiContext.CurrUserId, msgid);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 推送绑定
        /// </summary>
        public void BindMemberPush(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int device_type = dic.GetValue<int>("device_type", 0);
            string device_token = dic.GetValue<string>("device_token");
            if (!EnumHelper.IsDefined<DeviceType>(device_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "设备类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (string.IsNullOrEmpty(device_token))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "设备号不能为空";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = PushMsgBiz.Instance.BindMemberPush(apiContext.CurrUserId, device_token, device_type);
            if (result)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "推送设备绑定失败";
            }
            apiContext.apiHttpResp = apiResponse;
        }
        #endregion
    }
}